﻿$path= Get-Location
Write-Host $path

if(Test-Path $path"\NuGet.exe")
{
    #Start-Process NuGet.exe "setApiKey 7a3459c0-d9ce-4c81-aa2f-3a9faf36a81d"
    $nugetApiKey = "oy2k34zw2ptta5dcd33a4j3gfgy4yraudorkmyoorruijy"
    $nugetSource = "https://www.nuget.org/api/v2/package"
    $binPath = Join-Path -Path $path -ChildPath "\src\bin"
	Write-Host $binPath
    $dllPath = Join-Path -Path $path -ChildPath "\src\bin\net8.0"
	Write-Host $dllPath
    #Write-Host $packagePath
    Write-Host "提取程序及版本号。。。"
    $fileVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($dllPath+"\IpcServiceFramework.Core.dll")
    $version= $fileVersion.FileVersion

    $ws = New-Object -ComObject WScript.Shell
	
	$wsr = $ws.popup("是否需要删除以前的NuGet包？", 0,"确认提示",4 + 64)
	if($wsr -eq 6){
		$major = $fileVersion.FileMajorPart
		$minor = $fileVersion.FileMinorPart
	
		if($fileVersion.FilePrivatePart -eq 0){ $build = $fileVersion.FileBuildPart - 1 }
		else{ $build = $fileVersion.FileBuildPart}

		if($fileVersion.FilePrivatePart -gt 0){ $private = $fileVersion.FilePrivatePart - 1 }

		$oldversion = "$major.$minor.$build.$private"
		if($fileVersion.FilePrivatePart -eq 0){ $oldversion = "$major.$minor.$build" }

		$oldver = Read-Host "回车直接删除此版本($oldversion)的NuGet包或者输入需要删除的NuGet包版本号"
		if($oldver -eq "" -or $oldver -eq $null){ $oldver = $oldversion}
		Write-Host "开始向(https://www.nuget.org)发送删除 NuGet 包命令。"
		Start-Process NuGet.exe "delete IpcServiceFramework.Core.Net $oldver -Source $nugetSource -ApiKey $nugetApiKey -NonInteractive" -NoNewWindow -Wait
		Start-Process NuGet.exe "delete IpcServiceFramework.Hosting.Net $oldver -Source $nugetSource -ApiKey $nugetApiKey -NonInteractive" -NoNewWindow -Wait
		Start-Process NuGet.exe "delete IpcServiceFramework.Client.Net $oldver -Source $nugetSource -ApiKey $nugetApiKey -NonInteractive" -NoNewWindow -Wait
		Start-Process NuGet.exe "delete IpcServiceFramework.Hosting.NamedPipe.Net $oldver -Source $nugetSource -ApiKey $nugetApiKey -NonInteractive" -NoNewWindow -Wait
		Start-Process NuGet.exe "delete IpcServiceFramework.Client.NamedPipe.Net $oldver -Source $nugetSource -ApiKey $nugetApiKey -NonInteractive" -NoNewWindow -Wait
		Start-Process NuGet.exe "delete IpcServiceFramework.Hosting.Tcp.Net $oldver -Source $nugetSource -ApiKey $nugetApiKey -NonInteractive" -NoNewWindow -Wait
		Start-Process NuGet.exe "delete IpcServiceFramework.Client.Tcp.Net $oldver -Source $nugetSource -ApiKey $nugetApiKey -NonInteractive" -NoNewWindow -Wait
	}
	Write-Host "开始向(https://www.nuget.org)推送NuGet包。"
	Write-Host "开始向(https://www.nuget.org)推送 $binPath\IpcServiceFramework.Core.Net.$version.nupkg"
	Start-Process NuGet.exe "push ""$binPath\IpcServiceFramework.Core.Net.$version.nupkg"" -Source ""$nugetSource"" -ApiKey $nugetApiKey" -NoNewWindow -Wait

    Write-Host "开始向(https://www.nuget.org)推送 $binPath\IpcServiceFramework.Hosting.Net.$version.nupkg"
	Start-Process NuGet.exe "push ""$binPath\IpcServiceFramework.Hosting.Net.$version.nupkg"" -Source ""$nugetSource"" -ApiKey $nugetApiKey" -NoNewWindow -Wait


	Start-Process NuGet.exe "push ""$binPath\IpcServiceFramework.Client.Net.$version.nupkg"" -Source ""$nugetSource"" -ApiKey $nugetApiKey" -NoNewWindow -Wait
	Start-Process NuGet.exe "push ""$binPath\IpcServiceFramework.Hosting.NamedPipe.Net.$version.nupkg"" -Source ""$nugetSource"" -ApiKey $nugetApiKey" -NoNewWindow -Wait
	Start-Process NuGet.exe "push ""$binPath\IpcServiceFramework.Client.NamedPipe.Net.$version.nupkg"" -Source ""$nugetSource"" -ApiKey $nugetApiKey" -NoNewWindow -Wait
	Start-Process NuGet.exe "push ""$binPath\IpcServiceFramework.Hosting.Tcp.Net.$version.nupkg"" -Source ""$nugetSource"" -ApiKey $nugetApiKey" -NoNewWindow -Wait
	Start-Process NuGet.exe "push ""$binPath\IpcServiceFramework.Client.Tcp.Net.$version.nupkg"" -Source ""$nugetSource"" -ApiKey $nugetApiKey" -NoNewWindow -Wait

	Write-Host "向(https://www.nuget.org)推送NuGet包完成。"
}
cmd /c "pause"
#Remove-Item -Recurse -Force $path"\Package"